//
//  UIViewController+Mango.m
//  Mango
//
//  Created by 胡超 on 2018/11/30.
//  Copyright © 2018 胡超. All rights reserved.
//

#import "UIViewController+Mango.h"

@implementation UIViewController(Mango)

- (void)alert:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelButton otherButtons:(NSArray *)otherButtons handler:(MGAlertHandler)handler {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self addAlertActionsTo:alertController cancelButton:cancelButton otherButtons:otherButtons handler:handler inputHandler:nil];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)alertInput:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelButton otherButtons:(NSArray *)otherButtons handler:(MGAlertInputHandler)handler configuration:(MGAlertInputConfiguration)configuration, ... {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (configuration) {
        [alertController addTextFieldWithConfigurationHandler:configuration];
        
        // other configurations...
        MGAlertInputConfiguration otherConfiguration = nil;
        va_list args;
        va_start(args, configuration);
        while ((otherConfiguration = va_arg(args, MGAlertInputConfiguration))) {
            if (otherConfiguration == nil) {
                break;
            }
            [alertController addTextFieldWithConfigurationHandler:otherConfiguration];
        }
        va_end(args);
    }
    [self addAlertActionsTo:alertController cancelButton:cancelButton otherButtons:otherButtons handler:nil inputHandler:handler];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)actionSheet:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelButton otherButtons:(NSArray *)otherButtons handler:(MGAlertHandler)handler {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    [self addAlertActionsTo:alertController cancelButton:cancelButton otherButtons:otherButtons handler:handler inputHandler:nil];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - Private

- (void)addAlertActionsTo:(UIAlertController *)alertController cancelButton:(NSString *)cancelButton otherButtons:(NSArray<NSString *>*)otherButtons handler:(MGAlertHandler)handler inputHandler:(MGAlertInputHandler)inputHandler {
    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    if (cancelButton) {
        [buttons addObject:cancelButton];
    }
    if (otherButtons.count > 0) {
        [buttons addObjectsFromArray:otherButtons];
    }
    for (NSInteger i=0; i<buttons.count; i++) {
        NSString *title = buttons[i];
        UIAlertActionStyle style = UIAlertActionStyleDefault;
        if (cancelButton && i == 0) {
            style = UIAlertActionStyleCancel;
        } else if ([title hasPrefix:@"D:"]) {
            title = [title substringFromIndex:2];
            style = UIAlertActionStyleDestructive;
        }
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:title style:style handler:^(UIAlertAction *action) {
            if (handler) {
                handler(i);
            }
            if (inputHandler) {
                inputHandler(i, alertController.textFields);
            }
        }];
        [alertController addAction:alertAction];
    }
}

@end
